define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/report/models'),
        utils = require('js/utils/tools'),
        moment = require('moment'),
        chart = require('js/utils/chart'),
        helper = require('js/apps/report/common');
    require('multiselect');

    var ActivityReportView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch',
            'change #searchActivityType': 'changeBetType'
        },
        initialize: function (options) {
            this.options = options;
            this.model = new models.ActivityReport();
            this.activity_types = [1,3,4,6,7,8,9,10,11,12,13,14,15,17,18,19,20];
            this.model.bind('change reset', this.renderWithData, this);
        },
        doSearch: function () {
            this.options.dateRange = helper.getDateRange();
            var searched = utils.getAllInput('#content');
            this.options.activity_type = parseInt($('#searchActivityType').val());
            searched.activity_type = this.options.activity_type;
            var bet_type = parseInt($('#searchBetType').val());
            if (isNaN(bet_type)){
                this.options.bet_type = "-1";
                searched.bet_type = "-1";
            }else{
                this.options.bet_type = bet_type;
                searched.bet_type = bet_type;
            }
            utils.saveSearched('report/activity_report', searched);
            this.load();
        },
        changeBetType : function () {
            var key = $('#searchActivityType').val();
            $("#searchBetType").empty();
            var option = '<option value="-1" <%= searched.bet_type == -1 ? \'selected\' : \'\' %>>所有(汇总数据)</option>' +
                '<option value="-2" <%= searched.bet_type == -2 ? \'selected\' : \'\' %>>所有(单条数据)</option>';
            // if(null != key && key == "-1"){
            //     option = '<option value="-1">所有(汇总数据)</option><option value="-2">所有(单条数据)</option>';
            // }
            if(null != key && key != "-1" && key != "-2") {
                var bet_type = window.BET_TYPE[key];
                $.each(bet_type, function(key, value) {
                    option += "<option value='" + key + "'>" + value + "</option>";
                });
            }
            $('#searchBetType').html(option);
        },
        render: function () {
            console.log('enter render function ');
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        renderWithData: function () {
            console.log('enter renderwithdata');
            this.$el.html(this.template({
                items: this.model.list,
                sum: this.model.sum,
                options: this.options,
                activity_types: this.activity_types,
                searched: utils.getSearched('report/activity_report'),
                bet_list: window.get_bet_list(utils.getSearched('report/activity_report').activity_type)
            }));
            helper.loadDatePicker(this.options);
            utils.multiselect('#activityTypeFilter', true, {
                'includeSelectAllOption': true
            });
            var that = this;
            utils.renderTable('main-list', {
                $orderby: this.options.$orderby || '-day',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    var desc = false;
                    if (field[0] == '-') {
                        desc = true;
                        field = field.substr(1);
                    }
                    that.model.list = _.sortBy(that.model.list, function (o) {
                        if (desc) return 0 - o[field];
                        return o[field];
                    });
                    that.renderWithData();
                }
            });
            // 饼图
            var details = this.model.details;
            var options1 = chart.getHighChartPieOptions();
            options1.title.text = '购彩金额分布';
            options1.series[0].data = [];
            var options2 = chart.getHighChartPieOptions();
            options2.title.text = '购彩人数分布';
            options2.series[0].data = [];
            _.each(window.LOTTERY_TYPE, function(v, k){
                var detail = details[k] || {};
                options1.series[0].data.push([v, detail['pay_total'] || 0]);
                options2.series[0].data.push([v, detail['pay_users'] || 0]);
            });

            $('#pay-total-dist').highcharts(options1);
            $('#pay-users-dist').highcharts(options2);

            return this;
        },
        load: function () {
            var data = {
                date_range: this.options.dateRange.join(','),
                activity_type: this.options.activity_type,
                bet_type: this.options.bet_type
            };
            this.model.fetch({
                reset: true,
                data: data,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                }
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "report/ActivityReport.html",
        render: function () {
            var today = moment().format('YYYY-MM-DD'),
                dateRange = [today, today],
                view = new ActivityReportView({
                    dateRange: dateRange,
                    activity_type: -1,
                    bet_type: -2
                });
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });
});